// package com.alibaba.druid.sql.dialect.ads.visitor;

// import com.alibaba.druid.DbType;
// import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddColumn;
// import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
// import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
// import com.alibaba.druid.sql.ast.statement.SQLShowColumnsStatement;
// import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey;
// import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
// import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;

// import java.util.List;

#include "AdsOutputVisitor.h"
#include "../../../../DbType.h"
#include "../../../ast/SQLObject.h"
#include "../../../ast/statement/SQLAlterTableAddColumn.h"
#include "../../../ast/statement/SQLAssignItem.h"
#include "../../../ast/statement/SQLCreateTableStatement.h"
#include "../../../ast/statement/SQLColumnDefinition.h"
#include "../../../ast/statement/SQLShowColumnsStatement.h"
#include "../../mysql/ast/MySqlPrimaryKey.h"
#include "../../mysql/ast/statement/MySqlCreateTableStatement.h"
#include "../../../../utils/listTran.h"

AdsOutputVisitor::AdsOutputVisitor(std::stringstream * appender) 
: SQLASTOutputVisitor(appender)
{
}

AdsOutputVisitor::AdsOutputVisitor(std::stringstream * appender, DbType_ptr dbType)
    : SQLASTOutputVisitor(appender, dbType)
{
}

AdsOutputVisitor::AdsOutputVisitor(std::stringstream * appender, BOOL_ptr parameterized)
    : SQLASTOutputVisitor(appender, parameterized)
{
}

bool AdsOutputVisitor::visit(SQLCreateTableStatement_ptr x)
{
  SQLASTOutputVisitor::printCreateTable(x, BOOL::TRUE);

  SQLAssignItem_list_ptr options = x->getTableOptions();
  if (options->size() > 0)
  {
    println();
    print0(ucase ? make_string_ptr("OPTIONS (") : make_string_ptr("options ("));
    // SQLObject_list_ptr  tmp = ListTran::tran_class<SQLAssignItem*, SQLObject_ptr >(options);

    SQLObject_list_ptr tmp = SQLObject_list_ptr();
    for (auto it : *options)
    {
      tmp->push_back(std::dynamic_pointer_cast<SQLObject>(it));
    }

    printAndAccept(tmp, make_string_ptr(", "));
    print(')');
  }

  return false;
}

// @Override
bool AdsOutputVisitor::visit(SQLAlterTableAddColumn_ptr x)
{
  print0(ucase ? make_string_ptr("ADD COLUMN ") : make_string_ptr("add column "));
  // SQLObject_list_ptr  tmp = ListTran::tran_class<SQLColumnDefinition*, SQLObject_ptr >(x->getColumns());

  SQLObject_list_ptr tmp = SQLObject_list_ptr();
  for (auto it : *x->getColumns())
  {
    tmp->push_back(std::dynamic_pointer_cast<SQLObject>(it));
  }

  printAndAccept(tmp, make_string_ptr(", "));
  return false;
}

// @Override
bool AdsOutputVisitor::visit(SQLShowColumnsStatement_ptr x)
{
  print0(ucase ? make_string_ptr("SHOW COLUMNS") : make_string_ptr("show columns"));

  if (x->getTable() != nullptr)
  {
    print0(ucase ? make_string_ptr(" IN ") : make_string_ptr(" in "));
    x->getTable()->accept(std::make_shared<SQLASTVisitor>(*this));
  }

  return false;
}

// @Override
void AdsOutputVisitor::endVisit(MySqlPrimaryKey_ptr x)
{
}

// @Override
void AdsOutputVisitor::endVisit(MySqlCreateTableStatement_ptr x)
{
}
